package com.decent.ejfadmin.job;

import com.aliyun.openservices.shade.com.alibaba.rocketmq.shade.com.alibaba.fastjson.JSONObject;
import com.aliyun.openservices.shade.com.alibaba.rocketmq.shade.com.alibaba.fastjson.TypeReference;
import com.decent.ejfadmin.common.bean.ExkFeeQueryResponseBean;
import com.decent.ejfadmin.common.enums.Operator;
import com.decent.ejfadmin.report.service.CollectCardProfitDailyReportService;
import com.decent.ejfadmin.utils.Httpclient;
import com.decent.ejfadmin.utils.PlatConfigUtil;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.apache.http.message.BasicNameValuePair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.*;

/**
 * 易销卡利润日报表
 *
 * @author JunLei He
 */
@Component
@Configurable
public class ExkProfitDailyJob {
    private static final Logger LOGGER = LoggerFactory.getLogger(ExkProfitDailyJob.class);
    @Autowired
    private CollectCardProfitDailyReportService collectCardProfitDailyReportService;
    /**
     * 默认销卡手续费(分)
     */
    private static final double DEFAULT_STANDARD_FEE = 0.02;
    /**
     * 会员编号映射关系   易缴费在易销卡的会员编号--易销卡在易代充的会员编号
     */
    public static final HashMap<String, String> CUSTOMER_NO_REFLECT = new HashMap<>();

    static {
        //EJF
        CUSTOMER_NO_REFLECT.put("13950", "302720");
        CUSTOMER_NO_REFLECT.put("14165", "302720");
        //EFW
        CUSTOMER_NO_REFLECT.put("13630", "313995");
        CUSTOMER_NO_REFLECT.put("14350", "313995");
        CUSTOMER_NO_REFLECT.put("18370", "313995");
    }

    @Scheduled(cron = "0 0 4 * * ?")
    public void doScheduledJob() {
        try {
            Date startTime = DateUtils.truncate(DateUtils.addDays(new Date(), -1), Calendar.DATE);
            collectCardProfitDailyReportService.writeRecord(startTime);
        } catch (Exception e) {
            LOGGER.error("销卡利润定时任务执行异常", e);
        }
    }

    /**
     * 查询代充费用
     *
     * @param startTime 时间
     * @param data      保存数据
     */
    public static void queryChargeFee(Date startTime, HashMap<String, BigDecimal> data) {
        BasicNameValuePair[] param = {new BasicNameValuePair("collectDate", DateFormatUtils.format(startTime, "yyyy-MM-dd"))};
        String chargeFeeQueryUrl = PlatConfigUtil.getProperty("card_supply_info.properties", "chargeFeeQueryUrl");
        String result = Httpclient.post(chargeFeeQueryUrl, Arrays.asList(param), "UTF-8", 30 * 1000, 30 * 1000);
        LOGGER.info("查询易代充费用返回结果[{}],查询参数[{}]", result, ArrayUtils.toString(param));
        List<ExkFeeQueryResponseBean> chargeFee = new ArrayList<>();
        if (StringUtils.isNotBlank(result)) {
            chargeFee = JSONObject.parseObject(result, new TypeReference<List<ExkFeeQueryResponseBean>>() {
            });
        }
        for (ExkFeeQueryResponseBean exkFeeQueryResponseBean : chargeFee) {
            String agentNo = exkFeeQueryResponseBean.getAgentNo();
            data.put(agentNo + Operator.MOBILE.getType(), exkFeeQueryResponseBean.getCmccFee());
            data.put(agentNo + Operator.UNICOM.getType(), exkFeeQueryResponseBean.getUnicomFee());
            data.put(agentNo + Operator.TELECOM.getType(), exkFeeQueryResponseBean.getTelecomFee());
        }
    }

    /**
     * 获取单位销卡手续费用
     *
     * @return
     */
    public static double getStandardFee() {
        String standardSaleFee = PlatConfigUtil.getProperty("card_supply_info.properties", "standard_sale_Fee");
        double standardFee = DEFAULT_STANDARD_FEE;
        if (NumberUtils.isParsable(standardSaleFee)) {
            double checkFee = Double.valueOf(standardSaleFee);
            if (checkFee > 0) {
                standardFee = checkFee;
            }
        }
        return standardFee;
    }

}
